package com.pianzhu.blockchainemr.mapper;

import com.pianzhu.blockchainemr.pojo.OperationLog;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

@Mapper
public interface OperationLogMapper {

    @Insert("INSERT INTO operation_log (" +
            "module, operation, description, request_url, request_method, " +
            "request_params, response_result, execution_time, status, " +
            "error_message, operator_id, operator_name, operator_type, " +
            "ip_address, user_agent, created_time" +
            ") VALUES (" +
            "#{module}, #{operation}, #{description}, #{requestUrl}, #{requestMethod}, " +
            "#{requestParams}, #{responseResult}, #{executionTime}, #{status}, " +
            "#{errorMessage}, #{operatorId}, #{operatorName}, #{operatorType}, " +
            "#{ipAddress}, #{userAgent}, NOW())")
    void insert(OperationLog log);

    @Select("SELECT * FROM operation_log " +
            "WHERE module LIKE CONCAT('%', #{module}, '%') " +
            "AND (operator_name LIKE CONCAT('%', #{keyword}, '%') OR description LIKE CONCAT('%', #{keyword}, '%')) " +
            "ORDER BY created_time DESC " +
            "LIMIT #{limit}")
    List<OperationLog> selectRecentActivities(
            @Param("module") String module,
            @Param("keyword") String keyword,
            @Param("limit") Integer limit
    );

    @Select("SELECT * FROM operation_log ORDER BY created_time DESC LIMIT #{limit}")
    List<OperationLog> selectAllRecentActivities(@Param("limit") Integer limit);
}